package com.jzy.message.activities;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.jzy.message.R;
import com.jzy.message.activities.adapter.MessageFollowingAdapter;
import com.jzy.message.activities.base.AppRequestInterface;
import com.jzy.message.activities.base.BaseFragment;
import com.jzy.message.activities.base.DialogWaiting;
import com.jzy.message.util.common.CommUtility;
import com.jzy.message.util.json.JsonUtil;

import org.xutils.http.RequestParams;

import java.util.LinkedHashMap;
import java.util.List;

/**
 * Created by Administrator on 2016/7/2.
 */
public class Message_FollowingFragment extends BaseFragment implements View.OnClickListener  {
    private final int CMD_GETFOLLOWINGMSG=0X001;

    private ListView lv_message_following;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.message_fragment_following, container, false);
        initView(view);

        return view;
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        dlgWaiting = DialogWaiting.createDialogWaiting(getActivity(), "数据加载中...");
        getFollowingMsg();
    }
    private void initView(View view)
    {
        lv_message_following=(ListView)view.findViewById(R.id.lv_message_following);
    }
    @Override
    public void onClick(View v) {
        switch(v.getId()){

            default:
                break;
        }
    }
    private void getFollowingMsg()
    {
        RequestParams params = new RequestParams(AppRequestInterface.sMessageFollowingUrl);
        //添加参数
        params.addQueryStringParameter("messageid","");
        //thisFragment.getActivity().dlgWaiting.show();
        requestDataGet(CMD_GETFOLLOWINGMSG, getActivity(), params);
    }
    @Override
    public void ParseData(int flag,String jsonstr)
    {
        switch (flag)
        {
            case CMD_GETFOLLOWINGMSG:
                parseMessageFollowing(jsonstr);
                break;
            default:
                break;
        }
    }
    private void parseMessageFollowing(String jsonstr)
    {
        try {
            if(!CommUtility.IsResponseCorrect(jsonstr))
            {
                CommUtility.showmsg(getActivity(),"data error");
                return;
            }
            List<LinkedHashMap<String, Object>> list;
            list= (List<LinkedHashMap<String, Object>>) JsonUtil.JSONToObj(jsonstr, List.class,"data");
            MessageFollowingAdapter adapter = new MessageFollowingAdapter(getActivity(), R.layout.message_fragment_following_item, list, list.size());
            lv_message_following.setAdapter(adapter);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
